/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.log.impl;

import java.sql.Timestamp;

import org.kopsox.comm.message.LoggingMessage;
import org.kopsox.environment.proxy.EnvironmentPropertiesEnum;
import org.kopsox.environment.proxy.KopsoxRegistry;
import org.kopsox.general.priority.LoggingPriority;
import org.kopsox.general.priority.LoggingPriorityEnum;
import org.kopsox.util.log.KopsoxLogger;

/**
 * @author Konrad Renner - 29.08.2010
 */
public class SystemOutLoggerImpl implements KopsoxLogger {
	
	private final Class<?> clazz;
	private final LoggingPriority priority;
	
	public SystemOutLoggerImpl(final Class<?> clazz){
		this.clazz = clazz;
		String prio = KopsoxRegistry.INSTANCE.getKopsoxProperties().getProperty(EnvironmentPropertiesEnum.LOGGER_PRIORITY.toString());
		if(prio == null) {
			this.priority = LoggingPriorityEnum.ALL.getPriority();
		}else {
			this.priority = LoggingPriorityEnum.valueOf(prio)==null?LoggingPriorityEnum.ALL.getPriority():LoggingPriorityEnum.valueOf(prio).getPriority();
		}
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.log.KopsoxLogger#isLogEnabeld(org.kopsox.general.priority.LoggingPriority)
	 */
	@Override
	public boolean isLogEnabeld(final LoggingPriority type) {
		if(type.getPriorityValue() >= priority.getPriorityValue()) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.log.KopsoxLogger#writeLogMessage(org.kopsox.comm.message.LoggingMessage)
	 */
	@Override
	public void writeLogMessage(final LoggingMessage message) {
		if(isLogEnabeld(message.getPriority())){
			System.out.println(new Timestamp(System.currentTimeMillis())+" - "+this.clazz.getName()+" - "+ message.getMessageSender() +" - "+message.getMessage());
		}
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.log.KopsoxLogger#endLog(java.lang.String, org.kopsox.general.priority.LoggingPriority, long)
	 */
	@Override
	public void endLog(String messageSender, LoggingPriority prio, long processStartTime) {
		long time = System.currentTimeMillis();
		long difference = time - processStartTime;
		if(isLogEnabeld(prio)){
			System.out.println(new Timestamp(time)+" - "+this.clazz.getName()+" - "+ messageSender +" - Logging ends, Time of Logging-Process "+difference+"ms");
		}
	}

	/* (non-Javadoc)
	 * @see org.kopsox.util.log.KopsoxLogger#startLog(java.lang.String, org.kopsox.general.priority.LoggingPriority)
	 */
	@Override
	public long startLog(String messageSender, LoggingPriority prio) {
		long time = System.currentTimeMillis();
		if(isLogEnabeld(prio)){
			System.out.println(new Timestamp(time)+" - "+this.clazz.getName()+" - "+ messageSender +" - Logging starts");
		}
		return time;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("SystemOutLoggerImpl[");
		sb.append("clazz=");
		sb.append(clazz);
		sb.append(",priority=");
		sb.append(priority);
		sb.append(']');
		
		return sb.toString();
	}
}
